package com.xiay.stringboot.handle;

import com.xiay.stringboot.Exception.EmployeeException;
import com.xiay.stringboot.doman.Result;
import com.xiay.stringboot.enums.ResultEnum;
import com.xiay.stringboot.util.ResultUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by H-209-FX-XY on 2018/5/9.
 */
@ControllerAdvice
public class ExceptionHandle {

    private  static Logger logger = LoggerFactory.getLogger(ExceptionHandle.class);

    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public Result handle(Exception e){
        if(e instanceof EmployeeException){
            EmployeeException employeeException = (EmployeeException) e;
            return ResultUtil.error(employeeException.getCode(),employeeException.getMessage());
        }
        logger.error(" [系统异常]  {}",e);
        return ResultUtil.error(ResultEnum.UNKOWN_ERROR);
    }
}
